MyQuests = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "AceComm-2.0", "AceDebug-2.0", "AceHook-2.1");

MyQuests:RegisterDB("MyQuestsDB", "MyQuestsDBPC", "char");

MyQuests:RegisterDefaults(
  "char", {
    QuestDiary = {},
    QuestLog = {},
    QuestHistory = {},
    
    TurninEnabled = true,
    AcceptEnabled = true,
  }
);

MyQuests:RegisterDefaults(
  "profile", {
    -- settings
  }
);

MyQuests.QuestCache = nil;

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:OnInitialize()
  -- Called when the addon is loaded
  
  self:SetCommPrefix("MyQuests");

  PanelTemplates_SetNumTabs(QuestLogFrame, 2);
  PanelTemplates_SetTab(QuestLogFrame, 1);
  
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:OnEnable()
  -- Called when the addon is enabled

  self:SetDebugging(true);

  -- enable the communications channels
  self:RegisterComm(self.commPrefix, "WHISPER");
  self:RegisterComm(self.commPrefix, "ZONE");

  --self:SendCommMessage("WHISPER", "Triah", "Command1", "fuck");
  --self:SendCommMessage("WHISPER", "Triah", "Command2", "shit");
  
  --self:RegisterEvent("BAG_UPDATE")
  --self:RegisterEvent("CHAT_MSG_CHANNEL_NOTICE");  ???
  --self:RegisterEvent("CHAT_MSG_COMBAT_HONOR_GAIN");
  self:RegisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");
  self:RegisterEvent("CHAT_MSG_SYSTEM");
  --self:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN");
  --self:RegisterEvent("CHAT_MSG_TEXT_EMOTE");
  --self:RegisterEvent("CHAT_MSG_TEXT_EMOTE");
  --self:RegisterEvent("DUEL_FINISHED");
  --self:RegisterEvent("PLAYER_MONEY");
  self:RegisterEvent("PLAYER_TARGET_CHANGED");
  --self:RegisterEvent("QUEST_LOG_UPDATE");
  --self:RegisterEvent("ZONE_CHANGED");
  
  --self:Hook("ChatFrame_MessageEventHandler");
  self:Hook("FriendsFrame_OnEvent", true);
  
  self:Hook("QuestLog_OnShow", true);
  self:Hook("QuestLog_OnHide", true);
  
  self:Hook("QuestFrame_OnHide", true);
  self:Hook("GetGreetingText", true);
  self:Hook("GetNumActiveQuests", true);
  self:Hook("GetNumAvailableQuests", true);
  self:Hook("GetAvailableTitle", true);
  self:Hook("GetActiveTitle", true);
  self:Hook("SelectAvailableQuest", true);
  self:Hook("SelectActiveQuest", true);
  self:Hook("CloseQuest", true);
  self:Hook("CompleteQuest", true);
  self:Hook("GetTitleText", true);
  self:Hook("GetQuestText", true);
  self:Hook("GetObjectiveText", true);
  self:Hook("GetProgressText", true);
  self:Hook("GetRewardText", true);
  self:Hook("GetNumQuestRewards", true);
  self:Hook("IsQuestCompletable", true);
  self:Hook("GetQuestReward", true);
  self:Hook("GetRewardMoney", true);
  self:Hook("GetNumQuestItems", true);
  self:Hook("AcceptQuest", true);
  self:Hook("DeclineQuest", true);
  self:Hook("GetRewardSpell", true);

  self:Hook("GetDailyQuestsCompleted", true);
  self:Hook("GetQuestLogTitle", true);
  self:Hook("GetNumQuestLogEntries", true);
  self:Hook("SelectQuestLogEntry", true);
  self:Hook("GetQuestLogQuestText", true);
  self:Hook("GetQuestLogTimeLeft", true);
  self:Hook("GetNumQuestLeaderBoards", true);
  self:Hook("GetQuestLogLeaderBoard", true);
  self:Hook("AbandonQuest", true);
  self:Hook("GetQuestLogSelection", true);
  self:Hook("SetAbandonQuest", true);
  self:Hook("GetAbandonQuestName", true);
  self:Hook("GetQuestLogRewardMoney", true);
  self:Hook("GetNumQuestLogRewards", true);
  self:Hook("GetNumQuestLogChoices", true);
  
  self:RegisterEvent("MYQUESTS_MSG_PING_REPLY");
  self:RegisterEvent("MYQUESTS_MSG_LIST_RECEIVED");
  self:RegisterEvent("MYQUESTS_MSG_QUEST_INFO");
  self:RegisterEvent("MYQUESTS_TURNIN_REQUEST");
  self:RegisterEvent("MYQUESTS_TURNIN_ACCEPTED");
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:OnDisable()
  -- Called when the addon is disabled
end



function MyQuests:QuestLogFrameTab_OnClick()
  local tab = this:GetID();
  
  PanelTemplates_SetTab(QuestLogFrame, this:GetID());

  if (tab == 1) then
    MyQuests.QuestLogBypass = nil;
  elseif (tab == 2) then
    MyQuests.QuestLogBypass = true;
    MyQuests:Print("click");
  end
  
  QuestLog_Update();
end


local options = {
  type = "group",
  args = {
    create = {
      type = "execute",
      name = "create",
      desc = "Quest Creation Wizard",
      func = function()
        MyQuests:ShowWizard();
      end
    },
    edit = {
      type = "range",
      name = "edit",
      desc = "Quest Edit Wizard",
      message = nil,
      get  = function()
      end,
      set  = function(v)
        MyQuests:Print(v);
        MyQuests:ShowWizard(v);
      end,
      min  = 1,
      max  = 100,
      step = 1
    },
    reset = {
      type = "execute",
      name = "reset",
      desc = "Reset MyQuests User Database",
      func = function()
        MyQuests.db.char.QuestDiary = {};
        MyQuests.db.char.QuestLog = {};
        MyQuests.db.char.QuestHistory = {};
      end
    },
    test = {
      type = "execute",
      name = "test",
      desc = "Comm Test",
      func = function()
        --MyQuests:SendWho("Triah");
      end
    },
  }
}
MyQuests:RegisterChatCommand({"/myquests", "/mq"}, options);


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QueuedPingWho = nil;
MyQuests.QueuedDuelWho = nil;
function MyQuests:SendWho(player)
  MyQuests.QueuedDuelWho = true;

  FriendsFrame:Hide()
  SetWhoToUI(1);
  SendWho("n-\"" .. player .. "\"");
end


function MyQuests:GetQuestById(questId)
end

function MyQuests:GetQuestByIndex(questIndex)
end